package 结构型模式.组合模式.AAA;

import java.util.ArrayList;
import java.util.List;

// 抽象组件 - 文件系统组件
interface FileSystemComponent {
    void display(String indent);
    int getSize();
}

// 叶子节点 - 文件
class File implements FileSystemComponent {
    private String name;
    private int size;
    
    public File(String name, int size) {
        this.name = name;
        this.size = size;
    }
    
    @Override
    public void display(String indent) {
        System.out.println(indent + "📄 " + name + " (" + size + "KB)");
    }
    
    @Override
    public int getSize() {
        return size;
    }
}

// 复合组件 - 文件夹
class Directory implements FileSystemComponent {
    private String name;
    private List<FileSystemComponent> children = new ArrayList<>();
    
    public Directory(String name) {
        this.name = name;
    }
    
    public void add(FileSystemComponent component) {
        children.add(component);
    }
    
    public void remove(FileSystemComponent component) {
        children.remove(component);
    }
    
    @Override
    public void display(String indent) {
        System.out.println(indent + "📁 " + name + " (总大小: " + getSize() + "KB)");
        for (FileSystemComponent component : children) {
            component.display(indent + "    ");
        }
    }
    
    //不是递归不属于自己调用自己
    @Override
    public int getSize() {
        int totalSize = 0;
        for (FileSystemComponent component : children) {
            totalSize += component.getSize();
        }
        return totalSize;
    }
}

// 客户端代码
public class FileSystemExample {
    public static void main(String[] args) {
        // 创建文件
        FileSystemComponent file1 = new File("简历.pdf", 200);
        FileSystemComponent file2 = new File("报告.docx", 150);
        FileSystemComponent file3 = new File("照片.jpg", 500);
        FileSystemComponent file4 = new File("笔记.txt", 50);
        
        // 创建文件夹并添加文件
        Directory documents = new Directory("文档");
        documents.add(file1);
        documents.add(file2);
        
        Directory pictures = new Directory("图片");
        pictures.add(file3);
        
        Directory misc = new Directory("杂项");
        misc.add(file4);
        
        // 创建根目录并添加子文件夹
        Directory root = new Directory("我的电脑");
        root.add(documents);
        root.add(pictures);
        root.add(misc);
        
        // 显示整个文件系统结构
        root.display("");
    }
}